﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;
using DTcms.BLL;
using DTcms.Common;
using DTcms.Web.interfaceDOC;
using Newtonsoft.Json;

namespace DTcms.Web.WxSdjs
{
    /// <summary>
    /// CodeHandler 的摘要说明
    /// </summary>
    public class CodeHandler : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string type = context.Request["type"]; //1:跑男注册, 2:用户注册
            string phone = context.Request["phone"]; //手机号
            Result<string> result = GetCodePhone(phone, type);
            if (result.IsOk)
                context.Session["newVilidCode"] = result.Data;
            result.Data = string.Empty;
            context.Response.Write(JsonConvert.SerializeObject(result));
            context.Response.End();
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        /// <summary>
        /// 获取手机验证码
        /// </summary>
        /// <param name="context"></param>
        private Result<string> GetCodePhone(string phone, string userType)
        {
            Result<string> result = new Result<string>();

            bool isTruePhone = System.Text.RegularExpressions.Regex.IsMatch(phone, @"^((1[34578]\d{9}))$");


            if (!isTruePhone)
            {
                result.Code = 3;
                result.Msg = "手机号不正确";
                return result;
            }

            bool phoneExist = true;
            if (userType == "1")
            {
                BLL.pao_peisong_user bllRunning = new pao_peisong_user();
                phoneExist = bllRunning.ExistsByPhone(phone);
            }
            else if (userType == "2")
            {
                BLL.pao_user_list bllUser = new BLL.pao_user_list();
                phoneExist = bllUser.ExistsByPhone(phone);
            }


            if (phoneExist)
            {
                result.Code = 2;
                result.Msg = "手机号已注册";
                return result;
            }
            //判断验证码发送的次数
            BLL.Pao_VerityCodeCount bllVerity = new BLL.Pao_VerityCodeCount();

            bool b = bllVerity.ExistsCountBytime(phone);
            if (!b)
            {
                result.Code = 4;
                result.Msg = "发送次数太多";
                return result;
            }

            PhoneCheckCode vilidCode = new PhoneCheckCode(phone);
            string newVilidCode = vilidCode.GetPhoneCheckCode();            

            Model.Pao_VerityCodeCount model = new Model.Pao_VerityCodeCount();
            model.P_vcc_Code = newVilidCode;
            model.P_vcc_Phone = phone;
            model.P_vcc_Time = DateTime.Now;
            bllVerity.Add(model);

            result.IsOk = true;
            result.Code = 1;
            result.Data = newVilidCode;

            return result;
        }
    }
}